﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class SelectProcess : BasePage
    {
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();
        private Sys_CompanyBLL scb = new Sys_CompanyBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!IsPostBack)
                {
                    if (Session["ProcessBufferMemory1"] != null)
                    {
                        txtName.Text = Session["ProcessBufferMemory1"].ToString();
                    }

                    Bind();
                }
            }
        }

        private void Bind()
        {
            string whereStr = "";
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                whereStr += " AND lcp.C_PROCESS_NAME like '%" + txtName.Text.Trim() + "%'";
            }
            ViewState["Where"] = whereStr;

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = lpb.GetRecordCount(Where);

            DataTable dtItem;

            dtItem = lpb.GetListByPage(Where, "N_PROCESS_ID DESC", PageIng.CurrentPage, PageIng.RowsPerPage);

            if (dtItem != null && dtItem.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = dtItem;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Session["ProcessBufferMemory1"] = txtName.Text;
            MessageBox.ResponseScript(this, "FORM_INSERT();");
            //Response.Redirect("InsertProcess.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            string pid = "";
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    pid = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                }
            }
            if (count == 1)
            {
                Session["ProcessBufferMemory1"] = txtName.Text;
                MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                //Response.Redirect("EditProcess.aspx?id=" + pid);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string pids = "";
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    pids += ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip + ",";
                }
            }
            if (string.IsNullOrEmpty(pids))
            {
                MessageBox.Show(this, "【温馨提示】请选择需要的删除项！");
                return;
            }
            pids = pids.Substring(0, pids.Length - 1);
            lpb.DELETE(pids, CurrentUserName);
            Bind();
            MessageBox.Show(this, "【温馨提示】删除成功！");
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// GridView 标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindProjectName(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        public void BindProjectName(bool iSsort)
        {
            string whereStr = "";
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                whereStr += " AND lcp.C_PROCESS_NAME like '%" + txtName.Text.Trim() + "%'";
            }
            ViewState["Where"] = whereStr;

            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;

            rowcount = lpb.GetRecordCount(Where);

            DataTable dtItem;

            dtItem = lpb.GetListByPage(Where, "N_PROCESS_ID DESC", PageIng.CurrentPage, PageIng.RowsPerPage);

            DataView view = dtItem.DefaultView;
            if (iSsort)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;//设置排序
            }

            grvShow.DataSource = view;

            grvShow.DataBind();
        }
    }
}